package com.hanlink.dsp.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.qztech.sdk.R;

import static com.hanlink.dsp.util.Constants.DIENABLED_ALPHA;


public class AdvanceTimeDelay extends LinearLayout {
    public final static int MAX_TIME_DELAY = 1000;
    public final static int MAX_TIME_DELAY_EZ = 800;
    private boolean isClick = false;
    private TextView mEtValue;
    private VerticalSeekBar mVsbChannel;
    private ImageButton mIbtnPlus;
    private ImageButton mIbtnSub;

    private int unit = 0;

    private float mValue;

    private OnSeekBarCallbacks mOnSeekBarCallbacks;


    public AdvanceTimeDelay(Context context) {
        super(context);
        initView(context);
    }

    public AdvanceTimeDelay(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public AdvanceTimeDelay(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    private long mLastChangedTs;

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(com.hanlink.res.R.layout.view_timedelay, this, true);
        mEtValue = view.findViewById(com.hanlink.res.R.id.et_edittext);
        mIbtnPlus = view.findViewById(R.id.ibtn_plus);
        mIbtnSub = view.findViewById(R.id.ibtn_sub);
        mVsbChannel = view.findViewById(R.id.vsb_channel);

        mVsbChannel.setMax(MAX_TIME_DELAY_EZ);
        mVsbChannel.setProgress(0);
        mVsbChannel.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            private int progress;

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (progress % 2 != 0) {
                    seekBar.setProgress(progress + 1);
                    return;
                }
                isClick = false;
                mValue = seekBar.getProgress() / 100f;
                mEtValue.setText(getValueWithUnit(mValue));
                if (mOnSeekBarCallbacks != null) {
                    if (System.currentTimeMillis() - mLastChangedTs > 150) {
                        mLastChangedTs = System.currentTimeMillis();
                        mOnSeekBarCallbacks.onProgressChanged(seekBar, progress, (Integer) AdvanceTimeDelay.this.getTag(), fromUser, true);
                    } else {
                        mOnSeekBarCallbacks.onProgressChanged(seekBar, progress, (Integer) AdvanceTimeDelay.this.getTag(), fromUser, !fromUser);
                    }
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                progress = seekBar.getProgress();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (!seekBar.isPressed() && !isClick) {
                    seekBar.setProgress(progress);
                }
                mValue = seekBar.getProgress() / 100f;
                mEtValue.setText(getValueWithUnit(mValue));
                if (mOnSeekBarCallbacks != null) {
                    mOnSeekBarCallbacks.onProgressChangeOver(seekBar, seekBar.getProgress(), (Integer) AdvanceTimeDelay.this.getTag());
                }
            }
        });

        mIbtnSub.setOnClickListener(onClickListener);
        mIbtnPlus.setOnClickListener(onClickListener);
        mEtValue.setOnClickListener(onClickListener);
    }

    OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {

            isClick = true;
            int id = v.getId();
            if (id == R.id.ibtn_plus) {
                if (mVsbChannel.getProgress() < mVsbChannel.getMax()) {
                    int progress = mVsbChannel.getProgress() + 2;
                    if (progress % 2 != 0) {
                        progress += 1;
                    }
                    mVsbChannel.setProgress(progress);
                }
            } else if (id == R.id.ibtn_sub) {
                if (mVsbChannel.getProgress() > 0) {
                    int progress = mVsbChannel.getProgress() - 2;
                    if (progress % 2 != 0) {
                        progress -= 1;
                    }
                    mVsbChannel.setProgress(progress);
                }
            } else if (id == com.hanlink.res.R.id.et_edittext) {
                unit = (unit + 1) % 3;
                mEtValue.setText(getValueWithUnit(mValue));
            }
        }
    };


    public void setEnabled(boolean enabled) {
        mVsbChannel.setEnabled(enabled);
        mIbtnPlus.setEnabled(enabled);
        mIbtnSub.setEnabled(enabled);
        mEtValue.setEnabled(enabled);
        mVsbChannel.setAlpha(enabled?1f:DIENABLED_ALPHA);
        mIbtnPlus.setAlpha(enabled?1f:DIENABLED_ALPHA);
        mIbtnSub.setAlpha(enabled?1f:DIENABLED_ALPHA);
    }

    /**
     * 设置选中后的数值
     */
    public void setValue(float value) {
        mValue = value;
        if (mEtValue != null) {
            mEtValue.setText(getValueWithUnit(value));
        }
        mVsbChannel.setProgress((int) (value * 100));
    }

    private String getValueWithUnit(float value) {
        switch (unit) {
            case 1:
                return (int) (value * 34) + "cm";
            case 2:
                return (int) (value * 34 * 0.39) + "in";
            default:
                return value + "ms";
        }
    }


    public void setOnSeekBarCallbacks(OnSeekBarCallbacks mOnSeekBarCallbacks) {
        this.mOnSeekBarCallbacks = mOnSeekBarCallbacks;
    }

    @Override
    public void setTag(Object tag) {
        super.setTag(tag);
        mVsbChannel.setTag(tag);
    }

    public int getValue() {
        return mVsbChannel.getProgress();
    }


    public interface OnSeekBarCallbacks {

        void onProgressChanged(SeekBar seekBar, int progress, int currentIndex, boolean fromUser, boolean sendCmd);

        void onProgressChangeOver(SeekBar seekBar, int progress, int currentIndex);

    }
}
